<template>
	<el-dialog v-model="control.visible" title="提示" width="500" :before-close="public_close">

		<section v-if="xsArrayUtils.length(sr.childFlogicIndexes)==1">
			<span>请确认是否删除该节点？</span>
		</section>

		<section v-if="xsArrayUtils.length(sr.childFlogicIndexes)>1">
			<div class="xp-content">删除后当前状态后，【A状态】在审核后单据状态修改为如下哪个状态？</div>

			<el-form>
				<el-form-item label="状态选择" label-position="right">
					<el-select v-model="control.targetFlogicId" placeholder="请选择" style="width: 100%">
						<el-option v-for="item in control.options" :key="item.value" :label="item.label"
							:value="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="状态名称" label-position="right" v-if="control.selectValue==-1">
					<el-input v-model="control.newStatusName" style="width: 100%" placeholder="请输入" />
				</el-form-item>
			</el-form>

		</section>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="public_close">取消</el-button>
				<el-button type="primary" @click="control_click">
					确认
				</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script>
	import config from "@/config/config.js";
	import xcHttpUtils from '@/library/xcore/js/XcHttpUtils.js';
	import xcViewUtils from '@/library/xcore/js/XcViewUtils.js';
	import {
		xsStringUtils,
		xsDateUtils,
		xsArrayUtils
	} from 'xstack-for-javascript';

	export default {
		props: {
			fstatusList: {
				type: Array,
				default: []
			},
			flogicList: {
				type: Array,
				default: []
			},
			data: {
				type: Object,
				default: {}
			},
		},
		watch: {
			'fstatusList': {
				handler(newVal, oldVal) {
					let that = this;
					// that.public_show();
				},
			},
		},
		data() {
			return {
				config: config,
				xsStringUtils: xsStringUtils,
				xsDateUtils: xsDateUtils,
				xsArrayUtils: xsArrayUtils,
				sp: {
					clickFstatusId: null,
					callbackFunc: null
				},
				sr: {
					parentFlogicIndexes: [],
					childFlogicIndexes: [],
				},
				control: {
					visible: false,
					selectValue: null,
					options: [],
					targetFlogicId: null,
					newStatusName: null,
				}
			}
		},
		created() {
			let that = this;
			that.xinit();
		},
		methods: {
			xinit() {
				let that = this;
				that.control.options = [];
				for (let index in that.fstatusList) {
					let item = that.fstatusList[index];
					that.control.options.push({
						value: item['id'],
						label: item['name'],
					});
				}
				that.control.options.push({
					value: -1,
					label: "【增加新状态】",
				});
			},
			public_show(clickFstatusId, callbackFunc) {
				let that = this;
				that.control.visible = true;
				that.sp.clickFstatusId = clickFstatusId;
				that.sp.callbackFunc = callbackFunc;


				that.sr.parentFlogicIndexes = xsArrayUtils.indexesOfByObjectKey(that.flogicList, 'pass_fstatus_id',
					that.sp.clickFstatusId);
				that.sr.childFlogicIndexes = xsArrayUtils.indexesOfByObjectKey(that.flogicList, 'flowfstatus_id', that.sp
					.clickFstatusId);
				console.log("find parent/child logic-node", that.flogicList, that.sp.clickFstatusId, that.sr
					.parentFlogicIndexes, that.sr.childFlogicIndexes);

				if (xsArrayUtils.length(that.sr.childFlogicIndexes) == 1) {
					that.control.targetFlogicId = that.flogicList[that.sr.childFlogicIndexes[0]].pass_fstatus_id;
				}
			},
			public_reset() {
				let that = this;
				that.public_show();
			},
			public_close() {
				let that = this;
				that.control.visible = false;
			},
			control_click() {
				let that = this;
				if(that.data.xcflowjson.fstatusBeginId==that.sp.clickFstatusId){
					alert('初始节点不能删除');
					return;
				}
				if(that.data.xcflowjson.fstatusEndId==that.sp.clickFstatusId){
					alert('结束节点不能删除');
					return;
				}
				// update parent floginList#pass_fstatus_id
				for (let i in that.sr.parentFlogicIndexes) {
					let index = that.sr.parentFlogicIndexes[i];
					that.flogicList[index]['pass_fstatus_id'] = that.control.targetFlogicId;
					console.log("update flogic", that.flogicList[index], 'pass_fstatus_id=' + that.control
						.targetFlogicId);
				}

				// delete children floginList#flowfstatus_id
				for (let i = that.sr.childFlogicIndexes.length; i > 0; i--) {
					let index = that.sr.childFlogicIndexes[i - 1];
					console.log("delete flogic", that.flogicList[index], index);
					xsArrayUtils.remove(that.flogicList, index, 1);
				}

				// delete status-node
				if (true) {
					let deleteFstatusIndex = xsArrayUtils.indexesOfByObjectKey(that.fstatusList, 'id', that.sp
						.clickFstatusId);
					xsArrayUtils.remove(that.fstatusList, deleteFstatusIndex, 1);
					console.log("delete fstatus", deleteFstatusIndex, that.fstatusList);
				}


				that.control.visible = false;

				that.sp.callbackFunc();
			}
		}
	}
</script>

<style lang="scss">
	.xp-content {
		padding-bottom: $uni-spacing-col-base;
	}
</style>